#pragma once

#include <vsg/all.h>
#include <vsgQt/Viewer.h>
#include <vsgQt/Window.h>
#include <vsgXchange/all.h>

#include "../ecs/query.hpp"

#include "qlib/all.h"

#include <QImage>

namespace qbe::image2d {

class ScreenshotHandler;

class Models {
 public:
  Models(ecs::Query* _query);
  Models(vsg::ref_ptr<vsg::WindowTraits> traits);

  static double default_factor;

  // disFactor控制缩放，越小图像越大，一般为1- default_factor
  QImage saveImage2d(vsg::ref_ptr<vsg::Node> node, std::string fileName, double disFactor = default_factor);
  QImage saveImage2d(qlib::Reference::s_ptr refer, std::string fileName, double disFactor = default_factor, bool useQuery = true);

  // 器件专用
  QImage saveImage2d(qlib::Reference::s_ptr refer, int width, int height, bool reset_back_color = true, double disFactor = default_factor,
                     bool useQuery = true);

  // 图层专用-弃用
  QImage saveImage2d(uint32_t layer, int width, int height, double disFactor = default_factor);

  // 启用
  QImage saveImage2d(std::function<std::vector<std::string>(uint32_t)>& _funvc, uint32_t layer, int width, int height,
                     double disFactor = default_factor);

  static Models* Instance();

  static void setInstance(Models*);

  void setQuery(ecs::Query* _qeury);

 private:
  std::function<std::vector<std::string>(uint32_t)> funvc;

  void init();

  void setBackColor(vsg::vec4 color);

  static Models* instance;

  void resize(int width, int height);

  ecs::Query* query;

  vsg::ref_ptr<vsg::WindowTraits> windowTraits;

  vsg::ref_ptr<vsg::Options> options;
  vsg::ref_ptr<vsg::Group> vsg_scene;
  vsg::ref_ptr<vsgQt::Viewer> viewer;
  vsgQt::Window* window;

  bool use_vkEvent;
  int numFrames;

  vsg::ref_ptr<vsg::Device> device;
  VkPhysicalDeviceDepthStencilResolveProperties depthStencilResolve_properites;
  vsg::ref_ptr<vsg::Camera> camera;
  vsg::ref_ptr<vsg::Event> event;
  ScreenshotHandler* screenshotHandler;
  vsg::ref_ptr<vsg::CommandGraph> commandGraph;

  vsg::ref_ptr<vsg::ViewportState> viewport;
  double width_half;
  double height_half;

  double width_real;
  double height_real;

  vsg::ref_ptr<vsg::RenderGraph> secondary_RenderGraph;

  bool resize_ed = false;

  static uint32_t count;
};

}  // namespace qbe::image2d